﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PIM_ATE.Electrical;

namespace PIM_ATE
{
    public partial class FrmConfigSelect : Form
    {
        public FrmConfigSelect(ClsDataUser usr)
        {
            InitializeComponent();
            m_userObj = usr;
        }

        ClsDataConfigSelectBuilder m_selectObj;
        ClsDataUser m_userObj;

        private void FrmConfigSelect_Load(object sender, EventArgs e)
        {
            m_selectObj = new ClsDataConfigSelectBuilder(GDefine.DBNamePIM);
            updateConfig();
        }

        private void updateConfig()
        {
            dgvPIM.DataSource = null;
            dgvElec.DataSource = null;
            ctl_treeProcode.Nodes.Clear();
            //List<ProcodeNode> pNodesProcodes = m_cfgBuilder.getNodeList();
            string[] lstMOID = m_selectObj.download();
            if (lstMOID == null)
            {
                MessageBox.Show("下载MOID列表失败");
                return;
            }

            TreeNode tNodesProcodes = new TreeNode();
            tNodesProcodes.Text = "MOID";

            for (int i = 0; i < lstMOID.Length; i++)
            {
                TreeNode node = new TreeNode();
                node.Name = lstMOID[i];
                node.Text = lstMOID[i];

                tNodesProcodes.Nodes.Add(node);
            }

            ctl_treeProcode.Nodes.Add(tNodesProcodes);
            ctl_treeProcode.Nodes[0].Expand();
        }

        private void ctl_treeProcode_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (ctl_treeProcode.SelectedNode != null)
            {
                TreeNode tNode = ctl_treeProcode.SelectedNode;
                if (tNode.Name == "") return;

                ClsDataResultForAntennaBuilder resultObj = new ClsDataResultForAntennaBuilder(GDefine.DBNamePIM);
                string sProcode = resultObj.getProcodeByMOID(tNode.Text);
                if (sProcode == "" || sProcode == null)
                {
                    MessageBox.Show("无法查询产品代码!");
                    return;
                }

                m_selectObj.construct(tNode.Text);
                List<bool> enablePIM = m_selectObj.downloadPIMSelect();
                if (enablePIM == null)
                {
                    MessageBox.Show("下载错误");
                    return;
                }
                if (enablePIM.Count > 0)
                    SetPimDataTable(sProcode, enablePIM, dgvPIM);
                else
                {
                    if (dgvPIM.DataSource != null)
                        ((DataTable)dgvPIM.DataSource).Clear();
                }
                    

                m_selectObj.construct(tNode.Text);
                List<bool> enableELEC = m_selectObj.downloadELECSelect();
                if (enableELEC == null)
                {
                    MessageBox.Show("下载错误");
                    return;
                }
                if (enableELEC.Count > 0)
                    SetElecDataTable(sProcode, enableELEC, dgvElec);
                else
                {
                    if (dgvElec.DataSource != null)
                        ((DataTable)dgvElec.DataSource).Clear();
                }

                ctl_lblTips.Text = tNode.FullPath.ToString();
            }
        }

        private void SetPimDataTable(string sProCode, List<bool> enableConfig, DataGridView dvgConfig)
        {
            DataTable dtConfig;
            ClsDataConfigLocalBuilder cfgObj = new ClsDataConfigLocalBuilder(GDefine.DBNamePIM);
            cfgObj.constructByProcode(sProCode);
            if (cfgObj.construct(sProCode, "1.0"))
            {//根据选择下载配置
                if (cfgObj.download(out dtConfig) == null)
                {
                    MessageBox.Show("配置错误：" + sProCode);
                    return;
                }
            }
            else
            {
                MessageBox.Show("未发现配置：" + sProCode);
                return;
            }

            dvgConfig.ReadOnly = true;
            dvgConfig.RowHeadersVisible = false;
            dvgConfig.AllowUserToAddRows = false;
            dvgConfig.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dvgConfig.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dvgConfig.MultiSelect = false;

            List<string> names = new List<string>();
            names.Add("测试项目");
            names.Add("测试频段");
            names.Add("RCU序列");
            names.Add("角度");
            names.Add("测试要求");

            dvgConfig.DataSource = dtConfig.DefaultView.ToTable(false, names.ToArray());
            initTable(dvgConfig, enableConfig);
        }

        private void SetElecDataTable(string sProCode, List<bool> enableConfig, DataGridView dvgConfig)
        {
            List<clsTestItem> lstConfig;
            DataTable dtConfig;
            var cfgObj = new Electrical.ClsDataSParamBuilder(GDefine.DBNamePIM);
            cfgObj.constructByProcode(sProCode);
            if (cfgObj.construct(sProCode, "1.0"))
            {//下载配置
                dtConfig = cfgObj.download(out lstConfig);
            }
            else
            {
                MessageBox.Show("未发现配置：" + sProCode);
                return;
            }

            dvgConfig.ReadOnly = false;
            dvgConfig.RowHeadersVisible = false;
            dvgConfig.AllowUserToAddRows = false;
            dvgConfig.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dvgConfig.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dvgConfig.MultiSelect = false;

            string[] m_arrShowList = new string[9];
            int n = 0;
            m_arrShowList[n++] = clsTestItem.chn_ParamName;
            m_arrShowList[n++] = clsTestItem.chn_Freq;
            //m_arrShowList[i++] = clsTestItem.chn_Band;
            m_arrShowList[n++] = clsTestItem.chn_PortA_Angle;
            m_arrShowList[n++] = clsTestItem.chn_PortB_Angle;
            m_arrShowList[n++] = clsTestItem.chn_PortA_RCU;
            m_arrShowList[n++] = clsTestItem.chn_PortB_RCU;
            m_arrShowList[n++] = clsTestItem.chn_PortA;
            m_arrShowList[n++] = clsTestItem.chn_PortB;
            m_arrShowList[n++] = clsTestItem.chn_FactSpec;

            dvgConfig.DataSource = dtConfig.DefaultView.ToTable(false, m_arrShowList);
            initTable(dvgConfig, enableConfig);
        }

        private void initTable(DataGridView dvgConfig, List<bool> enableConfig)
        {
            if (dvgConfig.Columns[0].HeaderText != "")
            {
                var colCheck = new DataGridViewCheckBoxColumn();
                dvgConfig.Columns.Insert(0, colCheck);
            }

            if (dvgConfig.Rows.Count != enableConfig.Count)
            {//配置数量错误
                MessageBox.Show("可选配置错误,请重新配置！");
                return ;
            }

            for (int i = 0; i < dvgConfig.Rows.Count; i++)
            {
                dvgConfig.Rows[i].Cells[0].Value = enableConfig[i];
            }

            for (int i = 0; i < dvgConfig.Columns.Count; i++)
            {
                dvgConfig.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            //checkAll.Checked = true;
        }

        private void ctl_btnELEC_Click(object sender, EventArgs e)
        {
            //if (!m_userObj.HavePower(clsPowerManage.POWER_FEAFTURES.ELEC_CONFIG_SELECT))
            //{
            //    MessageBox.Show("权限不够!");
            //    return;
            //}

            FrmConfigNewSelect frmSel = new FrmConfigNewSelect(1);
            if (frmSel.ShowDialog() == DialogResult.OK)
            {
                updateConfig();
            }
        }

        private void ctl_btnPIM_Click(object sender, EventArgs e)
        {
            //if (!m_userObj.HavePower(clsPowerManage.POWER_FEAFTURES.PIM_CONFIG_SELECT))
            //{
            //    MessageBox.Show("权限不够!");
            //    return;
            //}

            FrmConfigNewSelect frmSel = new FrmConfigNewSelect(0);
            if (frmSel.ShowDialog() == DialogResult.OK)
            {
                updateConfig();
            }
        }
    }
}
